package com.huikeportal.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huikeportal.model.BrandModel;
import com.huikeportal.model.MulSiteCondition;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteCommon;
import com.huikeportal.model.SiteSelectParam;
import com.huikeportal.model.UserInfo;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;

/**
 * 品牌选择(多选)
 * 
 * @author winner
 * 
 */
@Controller
public class BrandSelectController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private SelectSiteService selectSiteService;

	@Autowired
	private HttpServletRequest req;

	@InitBinder("siteSelectParam")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("siteSelectParam.");
	}

	@RequestMapping("/brandSelectLoad")
	public String brandSelectLoad(SiteSelectParam siteSelectParam,
			ModelMap model) throws Exception {
		// 两种传参方式：form表单提交、转发跳转
		if ("".equals(siteSelectParam.getControlTo())
				|| siteSelectParam.getControlTo() == null) {
			siteSelectParam = (SiteSelectParam) (req
					.getAttribute("siteSelectParam"));
		}
		// 如果参数为空，直接返回登录界面
		if ("".equals(siteSelectParam.getControlTo())
				|| siteSelectParam.getControlTo() == null) {
			return "index";
		}
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		String menuHtml = loginService.getMenuHtml(userInfo, siteSelectParam
				.getPageFrom());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		// 获取登录用户的所有广场权限
		MulSiteCondition mulSiteCondition = new MulSiteCondition();
		mulSiteCondition.setGcList(loginService.getGcSiteList(userInfo));
		// 如果是从更换广场页面返回
		if (!"".equals(siteSelectParam.getBackGcSiteString())
				&& siteSelectParam.getBackGcSiteString() != null) {
			List<SiteAllInfo> gcList = new ArrayList<SiteAllInfo>();
			String[] gcArr = siteSelectParam.getBackGcSiteString().split(",");
			for (int i = 0; i < gcArr.length; i++) {
				SiteAllInfo siteInfo = new SiteAllInfo();
				SiteCommon site = selectSiteService.querySiteInfo(gcArr[i]);
				siteInfo.setSiteid(site.getSiteId());
				siteInfo.setSitename(site.getSiteName());
				gcList.add(siteInfo);
			}
			mulSiteCondition.setGcList(gcList);
		}
		mulSiteCondition.setDpList(loginService.getDpSiteList(userInfo));
		mulSiteCondition = selectSiteService.brandSelectLoad(mulSiteCondition);
		model.addAttribute("mulSiteCondition", mulSiteCondition);
		model.addAttribute("siteSelectParam", siteSelectParam);
		model.addAttribute("pageHead", "品牌分析");
		return "brandSelect";
	}

	/**
	 * ajax查询品牌信息
	 * 
	 * @param gcSiteString
	 * @param brandName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("ajaxSearchBrand")
	@ResponseBody
	public Map<String, Object> ajaxSearchBrand(String gcSiteString,
			String brandName) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		BrandModel brandModel = new BrandModel();
		// 品牌所属广场
		brandModel.setGcSiteString(gcSiteString);
		// 品牌名称
		if (brandName != null && !"".equals(brandName)) {
			brandModel.setBrandName(URLDecoder.decode(brandName, "UTF-8"));
		}
		List<BrandModel> brandList = selectSiteService.getBrandList(brandModel);
		map.put("brandList", brandList);
		return map;
	}

}
